﻿using ArticleClientLib;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 软件文管理
{
    public partial class ArticleControl : UserControl
    {
        ILogger log = LogManager.GetCurrentClassLogger();
        void view(string msg) => log.Info(msg);

        private ArticleRootDir root;

        public ArticleControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 更新显示
        /// </summary>
        /// <param name="root"></param>
        public void UpdateView(ArticleRootDir root)
        {
            this.root = root;
            //显示分类
            updateClassifly();

        }

        private void updateClassifly()
        {
            Invoke(() =>
            {
                classiflyView.Nodes.Clear();

                foreach (var c in root.Classiflies)
                {
                    ClassiflyJson json = c.ReadJson();
                    TreeNode tn = new TreeNode($"{json.name}[ID-{json.id}]");
                    tn.Tag = c;
                    classiflyView.Nodes.Add(tn);
                }
                view("已更新分类列表");
            });
        }

        private void ArticleControl_Load(object sender, EventArgs e)
        {

        }

        private void classiflyView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = classiflyView.SelectedNode;
            SelectedClassifly = node.Tag as Classifly;

            //引发事件
            ClassiflySelectChangeEvent?.Invoke(SelectedClassifly);

            //显示分类下的软文
            UpdateArticle();
        }

        /// <summary>
        /// 当前选中的分类
        /// </summary>
        public Classifly? SelectedClassifly { get; set; }


        public void UpdateArticle()
        {
            if (SelectedClassifly == null) return;


            articleView.Nodes.Clear();
            view("正在更新软文列表..");

            foreach (LocalArticle a in SelectedClassifly.Articles)
            {
                TreeNode tn = new TreeNode($"{a.Article.title}");
                tn.Tag = a;
                articleView.Nodes.Add(tn);
            }
            view("软文列表巳更新");
        }

        private void articleView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = articleView.SelectedNode;
            SelectedArticle = node.Tag as LocalArticle;

            //引发事件
            ArticleSelectChangeEvent?.Invoke(SelectedArticle);
        }

        /// <summary>
        /// 当前选中的软文
        /// </summary>
        public LocalArticle SelectedArticle { get; set; }


        #region 软文搜索


        /// <summary>
        /// 搜索（显示结果）
        /// </summary>
        /// <param name="key"></param>
        public void Search(string key)
        {

        }

        #endregion


        /// <summary>
        /// 当前分类选择改变时引发
        /// </summary>
        public event ClassiflySelectChangeEventDelegate ClassiflySelectChangeEvent;


        /// <summary>
        /// 软文选择改变时引发
        /// </summary>
        public event ArticleSelectChangeEventDelegate ArticleSelectChangeEvent;
    }

    public delegate void ClassiflySelectChangeEventDelegate(Classifly c);
    public delegate void ArticleSelectChangeEventDelegate(LocalArticle c);
}
